package org.example.controller;

import org.example.entity.Book;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/book")
//查询或者显示表单
public class BookController {
    @GetMapping("/checkBookList")
    public List<Book> checkBookList(String bookName){
        if(bookName!=null&&!bookName.equals("")){//如果是查询
            List<Book> newList =new ArrayList<>();
            for(Book book:bookList){
                if(book.getBookName().contains(bookName)){
                    newList.add(book);
                }
            }
            return newList;
        }
    return bookList;//若不是查询或者查询结果为空则直接将原表提供给前端
    }
    //添加书
    @PostMapping("/addBookList")
    public void addBookList(@RequestBody Book book){
            book.setId(bookList.size()+1);
            book.setState(0);
            bookList.add(book);
    }
    static List<Book> bookList=new ArrayList<Book>();
    static {
        bookList.add( new Book(1,"《百年孤独》","加西亚·马尔克斯",0));
        bookList.add( new Book(2,"《红楼明》","曹雪芹",1));
        bookList.add( new Book(3,"《西游记》","吴承恩",0));
        bookList.add( new Book(4,"《或者》","余华",0));
        bookList.add( new Book(5,"《简爱》","夏洛蒂·勃朗特",1));
    }
}

